package com.ebring.shareEducation.service.impl;

import com.ebring.shareEducation.bean.Comments;
import com.ebring.shareEducation.bean.CommentsReply;
import com.ebring.shareEducation.dao.CommentsMapper;
import com.ebring.shareEducation.service.CommentsService;
import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class CommentsServiceImpl implements CommentsService {

	@Autowired
	private CommentsMapper commentsMapper;
/*
	@Autowired
	private CommentsReplyMapper commentsReplyMapper;*/


	// 查询所有的留言
	@Override
	public List<Comments> selectComments() {
		return commentsMapper.selectAll();
	}

	// 修改留言
	@Override
	public RspMsg<Comments> updateComments(Comments comments) {
		RspMsg rspMsg = new RspMsg();
		int i = commentsMapper.updateByPrimaryKeySelective(comments);
		if (i == 0) {
			rspMsg.setError();
			rspMsg.setMsg("修改失败");
			return rspMsg;
		}
		rspMsg.setOK();
		return rspMsg;

	}

	// 根据ID删除留言

	@Override
	public RspMsg<Comments> deleteById(String commentsId) {
		RspMsg rspMsg = new RspMsg();
		int i = commentsMapper.deleteByPrimaryKey(commentsId);
		if (i == 0) {
			rspMsg.setError();
			rspMsg.setMsg("删除失败");
			return rspMsg;
		}

		rspMsg.setOK();
		return rspMsg;
	}

	// 新增留言
	@Override
	@Transactional
	public RspMsg<Comments> insertComments(Comments comments) {
		RspMsg rspMsg = new RspMsg();

		try {
		SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String commentId = UuidUtils.getUUID();//评论ID
		Date createtime = new Date();
        String createtimes=sim.format(createtime);
		comments.setCommentsId(commentId);
		comments.setCommentsState("0");//状态: 0 待审核，1通过，2不通过
		comments.setCreateTime(createtimes);
		comments.setCommentsTime(createtimes);

		int i = commentsMapper.insertSelective(comments);
			if (i>=1) {
				rspMsg.setOK();
			}

		} catch (Exception e) {
			e.printStackTrace();
			rspMsg.setError();
			rspMsg.setMsg("新增菜单失败");
			return rspMsg;
		}
		return rspMsg;
	}


	// 查询用户的留言
	@Override
	public List<Comments> selectUComments(Comments comments) {
		RspMsg rspMsg = new RspMsg();
		List<Comments> list =null;
		List<CommentsReply> replyList =null;

		if(comments !=null){
			list = commentsMapper.selectUComments(comments);
		}
		return list;
	}



}
